<template>
  <div class="container">
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
      <el-breadcrumb-item>菜单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-table :data="MenuList" style="width: 100%">
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="foodName" label="食物" width="180">
        </el-table-column>
        <el-table-column prop="foodTaste" label="口味" width="120">
          <!-- <template slot-scope="scope">
            <el-tag :key="i" v-for="(item,i) in scope.row.delicious">{{item}}</el-tag>
          </template> -->
        </el-table-column>
        <el-table-column prop="menuName" label="菜单分类" width="120"></el-table-column>
        <el-table-column prop="foodPrice" label="单价" width="120"></el-table-column>
        <el-table-column prop="foodText" label="描述"></el-table-column>
        <el-table-column prop="foodSales" label="销量" width="120"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip
              class="item"
              effect="dark"
              content="修改菜品"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini" @click="editFood(scope.row)"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              class="item"
              effect="dark"
              content="删除"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini" @click="deleteFood(scope.row.foodID)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
    </el-card>


    <!-- 修改菜品信息对话框 -->
    <el-dialog
  title="修改菜品"
  :visible.sync="EditDialogVisible"
  width="50%">
  <el-form :model="toEditFood" :rules="editFormRules" ref="editFormRef" label-suffix=":">
        <el-form-item label="菜品名称" prop="foodName">
          <el-input v-model="toEditFood.foodName"></el-input>
        </el-form-item>
        <el-form-item label="菜品价格" prop="foodPrice">
          <el-input v-model="toEditFood.foodPrice"></el-input>
        </el-form-item>
        <el-form-item label="口味">
          <el-input v-model="toEditFood.foodTaste"></el-input>
        </el-form-item>
        <el-form-item prop="menuID" label="菜单分类">
          <el-select
            v-model="toEditFood.menuID"
            placeholder="请选择菜单分类"
          >
            <el-option
              v-for="item in menuCates"
              :key="item.menuID"
              :label="item.menuName"
              :value="item.menuID"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜品描述">
          <el-input v-model="toEditFood.foodText"></el-input>
        </el-form-item>
        <el-form-item label="图片">
            <el-image
              style="width: 100px; height: 100px"
              :src="toEditFood.foodImage"
            ></el-image>
          <el-upload
            :action="uploadUrl"
            :on-preview="handlePreview"
            :on-success="unploaded"
            multiple
            :limit="1"
            list-type="picture"
          >
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传一个jpg/png文件</div>
          </el-upload>
        </el-form-item>
      </el-form>
  <span slot="footer" class="dialog-footer">
    <el-button @click="EditDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="submitEdit">确 定</el-button>
  </span>
</el-dialog>
  </div>
</template>
<script>
export default {
  data() {
     //定义价格规则
    var checkPrice = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('年龄不能为空'));
        }
          if (!Number(value)) {
            return callback(new Error('请输入数字值'));
          } 
      };
    return {
      MenuList: [],
      EditDialogVisible: false,
      // 需要修改的食物
      toEditFood: {

      },
      editFormRules: {
        foodName: [
          { required: true, message: "请输入菜品名称", trigger: "blur" },
          { min: 2, max: 10, message: "长度在 2 到 10 个字符", trigger: "blur" }
        ],
        foodPrice: [{ validator: checkPrice, trigger: 'blur' }],
        menuID: [
          { required: true, message: "请选择菜单分类", trigger: "change" }
        ]
      },
      menuCates: [],
      //图片上传地址
      uploadUrl: "http://47.107.82.159:18081/food/img",
      //预览图片对话框
      showImgDialogVisible: false
    }
  },
  created() {
    this.getMenuList()
  },
  methods: {
    //获取菜单分类列表
    async getMenuCateList() {
      const { data: res } = await this.$http.get("menu")
      this.menuCates = res.data
    },
    async getMenuList() {
      // 发请求
      const { data: res } = await this.$http.get("menu")
      this.menuCates = res.data
      console.log('res.data: ', res.data);
      const { data: res0 } = await this.$http.get('food')
      console.log('res0.data: ', res0.data);
      res0.data.forEach( v => {
        // v.menuName = ''
        let index = this.menuCates.findIndex(m => v.menuID == m.menuID)
        if(index == -1) return
        v.menuName = this.menuCates[index].menuName
      })
      this.MenuList = res0.data
    },
    //修改菜品信息
    editFood(food) {
      this.EditDialogVisible = true
      this.toEditFood = food
    },
    //删除菜品
    deleteFood(foodID) {
      this.$confirm('此操作将删除此菜品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          //?删除订单 
          const { data: res } = await this.$http.delete('food/'+foodID)
          this.getMenuList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    //点击图片预览功能
    handlePreview() {
      this.showImgDialogVisible = true
    },
    //上传 成功的钩子
    unploaded(res) {
      this.addForm.foodImage = res
    },
    //点击提交修改
    async submitEdit() {
      this.EditDialogVisible = false
      console.log(this.toEditFood);
      
      const { data: res } = await this.$http.put('food/'+this.toEditFood.foodID, {
        foodImage: this.toEditFood.foodImage,
        foodName: this.toEditFood.foodName,
        foodPrice: this.toEditFood.foodPrice,
        foodTaste: this.toEditFood.foodTaste,
        foodText: this.toEditFood.foodText,
        menuID: this.toEditFood.menuID
      }) 
      console.log(res);  
      this.$message({
          message: '修改菜品成功',
          type: 'success'
        });
        this.getMenuList()
      
    }
  }
}
</script>
<style lang="less" scoped>
.el-tag {
  margin-right: 8px;
}
</style>
